#! /usr/bin/python
# -*- coding: utf-8 -*-

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import aimg
import conf

class MainPage(webapp.RequestHandler):
    def get(self):
        imgId = self.request.get('id')
        try:
            imgId = int(imgId)
        except ValueError:
            imgId = 0
        contentType = conf.imageSupportContentTypes.get("jpg")
        imgData = ''
        imageModel = aimg.getImageById(imgId)
        if imageModel:
            contentType = imageModel.contentType
            imgData = imageModel.imgdata
        self.response.headers['Content-Type'] = contentType
        self.response.out.write(imgData)


application = webapp.WSGIApplication(
                                     [('/getimg', MainPage)],
                                     debug=True)
def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
